package com.beiing.tianshuai.tianshuai.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.app.HttpRequestConstant;
import com.beiing.tianshuai.tianshuai.base.BaseRecyclerViewAdapter;
import com.beiing.tianshuai.tianshuai.entity.SearchFriendBean;
import com.beiing.tianshuai.tianshuai.widget.CircleImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Jinni on 2017/8/16 0016.
 */

public class SearchFriendAdapter extends BaseRecyclerViewAdapter<SearchFriendBean.DataBean> {

    private Context mContext;
    private OnItemClickListener mListener;

    public void setListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public SearchFriendAdapter(Context context) {
        mContext = context;
    }

    @Override
    public int getItemCount() {
        if (mList != null) {
            return mList.size();
        } else {
            return 0;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View convertView = View.inflate(mContext, R.layout.item_search_friend_result_list, null);
        return new ViewHolder(convertView);
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        final SearchFriendBean.DataBean bean = mList.get(position);
        String avatar = bean.getPhoto();
        String name = bean.getName();
        String email = bean.getEmail();
        RequestOptions requestOptions = new RequestOptions()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .override(350, 350);
        ViewHolder viewHolder = (ViewHolder) holder;
        Glide.with(mContext.getApplicationContext())
                .load(TextUtils.isEmpty(avatar) ? R.mipmap.default_avatar : HttpRequestConstant.USER_PIC_HEAD + avatar)
                .apply(requestOptions)
                .into(viewHolder.mCivUserAvatar);
        viewHolder.mTvUserNumber.setText(email);
        viewHolder.mTvUserName.setText(name);
        viewHolder.mItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.onItemClickListener(bean.getUid());
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        View mItem;
        @BindView(R.id.civ_user_avatar)
        CircleImageView mCivUserAvatar;
        @BindView(R.id.tv_user_nickname)
        TextView mTvUserName;
        @BindView(R.id.tv_user_number)
        TextView mTvUserNumber;

        public ViewHolder(View convertView) {
            super(convertView);
            ButterKnife.bind(this, convertView);
            mItem = convertView;
        }
    }

    public interface OnItemClickListener {
        void onItemClickListener(String uid);
    }
}
